package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.entity.config.goods.MammonSkill;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.curio.enums.eCurioConditionType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.module.activity.curio.CurioModule;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.decoration.UserMammon;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.MammonModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.mammon.MammonMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.S_MAMMON_WORSHIP, desc = "膜拜财神")
public class MammonWorshipCmd extends AbstractCommand {

	DecorationProto.MammonWorshipRespMsg.Builder respMsg ;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		respMsg = mammonWorship(player);
	}

	@Override
	public void sendData(GamePlayer player) {
		player.sendPacket(Protocol.U_MAMMON_WORSHIP, respMsg);
	}

	/**
	 * 膜拜财神
	 * @param player
	 * @return
	 */
	public static DecorationProto.MammonWorshipRespMsg.Builder mammonWorship(GamePlayer player){
		DecorationProto.MammonWorshipRespMsg.Builder respMsg = DecorationProto.MammonWorshipRespMsg.newBuilder();
		UserMammon userMammon = player.getModule(MammonModule.class).getUserMammon();
		if(null == userMammon){
			respMsg.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
			return respMsg;
		}
		long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
		if(zeroTime <= userMammon.getWorshipTime()){
			respMsg.setRet(GameErrorCode.E_MAMMON_HAS_WORSHIP);
			return respMsg;
		}
		List<Integer> localMammonDecorationIdList = new ArrayList<>();
		Map<Integer, List<UserDecoration>> localDataMap = new ConcurrentHashMap<>();
		for(int mammonDecorationId : MammonConfigMgr.getLocalMammonDecorationIdList()){
			List<UserDecoration> dataList = MammonMgr.getUserDecorationList(mammonDecorationId);
			if(dataList.size() > 0) {
				localDataMap.put(mammonDecorationId, dataList);
				localMammonDecorationIdList.add(mammonDecorationId);
			}
		}
		ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
		Map<Integer, DecorationProto.MammonListMsg> crossMammonMap = null;
		List<Integer> crossMammonDecorationIdList = new ArrayList<>();
		if(null != serverGroup && serverGroup.getCrossMammonState() > 0){
			crossMammonMap = CrossDataMgr.getCrossGroupMammonMsgMap();
			for(Map.Entry<Integer, DecorationProto.MammonListMsg> dataEntry : crossMammonMap.entrySet()){
				if(dataEntry.getValue().getMammonListCount() > 0){
					crossMammonDecorationIdList.add(dataEntry.getKey());
				}
			}
		}
		RandomHelper threadSafeRandom = new RandomHelper();
		List<Integer> randomList = new ArrayList<>(localMammonDecorationIdList);
		randomList.addAll(crossMammonDecorationIdList);
		if(randomList.size() <= 0){
			if(null != serverGroup && serverGroup.getCrossMammonState() > 0){
				randomList = MammonConfigMgr.getMammonDecorationIdList();
			}else {
				randomList = MammonConfigMgr.getLocalMammonDecorationIdList();
			}
		}
		int index = threadSafeRandom.next(0, randomList.size());
		int mammonDecorationId = randomList.get(index);

		DecorationProto.MammonTempMsg.Builder mammonMsg = null;

		if (MammonConfigMgr.isCrossMammonDecorationId(mammonDecorationId)
				|| MammonConfigMgr.isCrossAreaMammonDecorationId(mammonDecorationId)) {
			if(null != crossMammonMap) {
				DecorationProto.MammonListMsg listMsg = crossMammonMap.get(mammonDecorationId);
				if(null != listMsg && listMsg.getMammonListCount() > 0){
					index = threadSafeRandom.next(0, listMsg.getMammonListCount());
					mammonMsg = listMsg.getMammonList(index).toBuilder();
				}
			}
		}else {
			if(localDataMap.size() > 0) {
				List<UserDecoration> dataList = localDataMap.get(mammonDecorationId);
				index = threadSafeRandom.next(0, dataList.size());
				UserDecoration userDecoration = dataList.get(index);
				mammonMsg = DecorationPb.parseMammonTempMsg(userDecoration, UserMgr.parseUserBaseInfo(UserMgr.getUserInfo(userDecoration.getUserId()),
						GameServer.getInstance().getServerId()));
			}
		}
		MammonSkill mammonSkill = null;
		//首次参拜不触发财神爷效果
		if(userMammon.getTotalWorshipTimes() == 0) {
			do{
				mammonSkill = ConfigMgr.getRandomMammonSkill();
				SkillInfo skillInfo = SkillMgr.getSkillInfo(mammonSkill.getMammonSkillId());
				if(skillInfo.getType() != eSkillType.MammonRankWorshipAddIngots.getValue()){
					break;
				}
			}while (true);
		}else {
			mammonSkill = ConfigMgr.getRandomMammonSkill();
		}
		userMammon.setMammonSkillId(mammonSkill.getMammonSkillId());
		userMammon.setTotalWorshipTimes(userMammon.getTotalWorshipTimes() + 1);
		userMammon.setWorshipTime(System.currentTimeMillis() / 1000);
		Property reward = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(mammonSkill.getAddIngots()));
		//
		reward.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(CurioMgr.getSkillAdditionBySkillType(eSkillType.CurioMammonLikeIngotsAddSkill.getValue(), player, 0)));
		player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Mammon, eLogMoneyType.MammonWorship);
		player.notifyListener(eGamePlayerEventType.MammonWorshipTimes.getValue(), 1);
		player.notifyListener(eGamePlayerEventType.MammonTotalWorshipTimes.getValue(), userMammon);
		LogMgr.addLogMammonWorship(player.getUserId(), mammonSkill.getMammonSkillId(), PropertyHelper.parsePropertyToString(reward));
		//DataAnalyticsMgr.trackMammonWorshipPray(player);
		respMsg.setRet(0);
		if(null != mammonMsg) {
			respMsg.setMammon(mammonMsg);
		}
		respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
		DecorationProto.MammonDataMsg.Builder dataMsg = DecorationPb.parseMammonDataMsg(userMammon);
		if(null != dataMsg){
			respMsg.setMammonData(dataMsg);
		}
		player.getModule(CurioModule.class).addUserCurioCountData(eCurioConditionType.GiveLikeMammonTimes.getType(), 1);
		return respMsg;

	}

}
